package com.example.apricots.Net;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.apricots.Net.Request.RLogin;
import com.example.apricots.Net.Request.RRegist;
import com.example.apricots.Net.bean.CommonBean;
import com.example.apricots.bean.User;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HttpClient {


    private static final String TAG = HttpClient.class.getSimpleName();

    /**
     * 获取用户信息
     *
     * @param callback
     */
    public static void login(int id, RLogin login, final HttpResponseHandlerCallback<ResponseBody> callback) {
        RequestApi.getInstance().getRequestAdapter().login(id, login).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body != null) {
                    callback.success(body);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                callback.faile();
            }
        });

    }

    public static void addUser(User user, final HttpResponseHandlerCallback<ResponseBody> callback) {
        RRegist rRegist = new RRegist(user.getUsername(),
                user.getPassword(),
                user.getAddress(),
                user.getSex(),
                user.getAge(),
                user.getHeight());
        RequestApi.getInstance().getRequestAdapter().addUser(rRegist).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body != null) {
                    callback.success(body);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                callback.faile();
            }
        });

    }

}
